<script type="text/javascript" src="<?=PATH_URL.'static/js/admin/jquery.slugit.js'?>"></script>
<script type="text/javascript">
$(document).ready( function() {
	$("#titleAdmincp").slugIt({
		events: 'keyup blur',
		output: '#slugAdmincp',
		space: '-'
	});
});

function save(){
	var options = {
		beforeSubmit:  showRequest,  // pre-submit callback
		success:       showResponse  // post-submit callback
    };
	$('#frmManagement').ajaxSubmit(options);
}

function showRequest(formData, jqForm, options) {
	var form = jqForm[0];
	if(form.userAdmincp.value == ''){
		$('#txt_error').html('Please enter username!!!');
		$('input').removeClass('error-fr');
		$('input[name="userAdmincp"]').addClass('error-fr');
		$('#loader').fadeOut(300);
		show_perm_denied();
		return false;
	}
	if(form.passAdmincp.value == ''){
		$('#txt_error').html('Please enter password!!!');
		$('#loader').fadeOut(300);
		show_perm_denied();
		$('input').removeClass('error-fr');
		$('input[name="passAdmincp"]').addClass('error-fr');
		return false;
	}

	if(form.emailAdmincp.value == ''){
		$('#txt_error').html('Please enter email!!!');
		$('#loader').fadeOut(300);
		show_perm_denied();
		$('input').removeClass('error-fr');
		$('input[name="emailAdmincp"]').addClass('error-fr');
		return false;
	}

	var filter = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
 	if (!filter.test($('#emailAdmincp').val())) {
 			$('#txt_error').html('Email incorrect!!!');
			$('#loader').fadeOut(300);
			show_perm_denied();
			$('input').removeClass('error-fr');
			$('#emailAdmincp').focus();
			$('input[name="emailAdmincp"]').addClass('error-fr');
			return false;
  }

	if(form.groupAdmincp.value == '0'){
		$('#txt_error').html('Please choose group user!!!');
		$('#loader').fadeOut(300);
		show_perm_denied();
		$('input').removeClass('error-fr');
		$('#groupAdmincp').focus();
		$('input[name="groupAdmincp"]').addClass('error-fr');
		return false;
	}
}

function showResponse(responseText, statusText, xhr, $form) {
	if(responseText=='success'){
		location.href=root+"admincp/"+module+"/#/save";
	}

	if(responseText=='error-username-exists'){
		$('#txt_error').html('Username already exists!!!');
		$('#loader').fadeOut(300);
		show_perm_denied();
		$('#userAdmincp').focus();
		$('input[name="userAdmincp"], textarea[name="userAdmincp"]').addClass('error-fr');
		return false;
	}

	if(responseText=='error-email-exists'){
		$('#txt_error').html('Email already exists!!!');
		$('#loader').fadeOut(300);
		show_perm_denied();
		$('#userAdmincp').focus();
		$('input[name="emailAdmincp"], textarea[name="emailAdmincp"]').addClass('error-fr');
		return false;
	}

	if(responseText=='permission-denied'){
		show_perm_denied();
	}
}
</script>
<div class="gr_perm_error" style="display:none;">
	<p><strong>FAILURE: </strong><span id="txt_error">Permission Denied.</span></p>
</div>
<div class="table">
	<div class="head_table"><div class="head_title_edit"><?=$module?></div></div>
	<div class="clearAll"></div>

	<form id="frmManagement" action="<?=PATH_URL.'admincp/'.$module.'/save/'?>" method="post" enctype="multipart/form-data">
	<input type="hidden" value="<?=$id?>" name="hiddenIdAdmincp" />
	<div class="row_text_field_first">
		<table cellspacing="0" cellpadding="0" border="0" width="100%">
			<tr>
				<td class="left_text_field">Status:</td>
				<td class="right_text_field"><input <?php if(isset($result->status)){ if($result->status==1){ ?>checked="checked"<?php }}else{ ?>checked="checked"<?php } ?> type="checkbox" class="custom_chk" name="statusAdmincp" /></td>
			</tr>
		</table>
	</div>

	<div class="row_text_field">
		<table cellspacing="0" cellpadding="0" border="0" width="100%">
			<tr>
				<td class="left_text_field">Username:</td>
				<td class="right_text_field"><input value="<?php if(isset($result->username)) { print $result->username; }else{ print '';} ?>" type="text" name="userAdmincp" id="userAdmincp" /></td>
			</tr>
		</table>
	</div>

	<div class="row_text_field">
		<table cellspacing="0" cellpadding="0" border="0" width="100%">
			<tr>
				<td class="left_text_field">Password:</td>
				<td class="right_text_field"><input value="" type="password" name="passAdmincp" id="passAdmincp" /></td>
			</tr>
		</table>
	</div>

	<div class="row_text_field">
		<table cellspacing="0" cellpadding="0" border="0" width="100%">
			<tr>
				<td class="left_text_field">Email:</td>
				<td class="right_text_field"><input value="<?php if(isset($result->email)) { print $result->email; }else{ print '';} ?>" type="text" name="emailAdmincp" id="emailAdmincp" onblur="" /></td>
			</tr>
		</table>
	</div>

	<div class="row_text_field">
		<table cellspacing="0" cellpadding="0" border="0" width="100%">
			<tr>
				<td class="left_text_field">Group:</td>
				<td class="right_text_field">
					<select name='groupAdmincp'>
						<option value='0'>[ Group ]</option>
						<?php if(isset($groups)) {
							foreach ($groups as $group) {
								?>
								<option value='<?=$group->id?>' <?php if(isset($result->group) && $result->group == $group->id) { print 'selected="selected"';	} ?>><?=$group->name_vi?></option>
						<?php } } ?>
					</select>
				</td>
			</tr>
		</table>
	</div>

	</form>
</div>

<script type="text/javascript">
	function check_email(){
		email = $('#emailAdmincp').val();
		var filter = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
   	if (!filter.test($('#emailAdmincp').val())) {
   			$('#txt_error').html('Email incorrect!!!');
				$('#loader').fadeOut(300);
				show_perm_denied();
				$('#titleAdmincp').focus();
				return false;
    }
	}
</script>
